<!--
  * BIP运算单行
  *
  * @Author:    AKITA
  * @Date:      2025-08-13 23:51:27
  * @Copyright  AKITA
-->
<template>
  <a-drawer :title="form.id ? '编辑' : '添加'" width="30%" :open="visibleFlag" @close="onClose" :maskClosable="false">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 4 }">
      <a-form-item label="电键" name="id">
        <span v-text="form.id"> </span>
      </a-form-item>
      <a-form-item label="行号" name="lineNum">
        <span v-text="form.lineNum"> </span>
      </a-form-item>
      <a-form-item label="行状态" name="lineStatus">
        <span v-text="form.lineStatus"> </span>
      </a-form-item>
      <a-form-item label="合同号" name="connum">
        <span v-text="form.connum"> </span>
      </a-form-item>
      <a-form-item label="物料编码" name="itemId">
        <span v-text="form.itemId"> </span>
      </a-form-item>
      <a-form-item label="物料名称" name="itemName">
        <span v-text="form.itemName"> </span>
      </a-form-item>
      <a-form-item label="规格" name="specs">
        <span v-text="form.specs"> </span>
      </a-form-item>
      <a-form-item label="单位" name="unit">
        <span v-text="form.unit"> </span>
      </a-form-item>
      <a-form-item label="在途数量" name="onOrderQty">
        <span v-text="form.onOrderQty"> </span>
      </a-form-item>
      <a-form-item label="库存数量" name="invQty">
        <span v-text="form.invQty"> </span>
      </a-form-item>
      <a-form-item label="需求数量" name="invQty">
        <span v-text="excute(form.reqQty)"> </span>
      </a-form-item>
      <a-form-item label="需求日期" name="reqDate">
        <span v-text="form.reqDate"> </span>
      </a-form-item>

      <a-form-item label="下单数量" name="qty">
        <span v-text="excute(form.qty)"> </span>
      </a-form-item>
      <a-form-item label="加量数量" name="addQty">
        <a-input-number style="width: 100%" v-model:value="form.addQty" :step="0.0001" :min="0.0000" placeholder="加量数量"
          @change="addQtyChange" />
      </a-form-item>
      <a-form-item label="来源" name="source">
        <a-select v-model:value="form.source">
          <a-select-option value="01">生产</a-select-option>
          <a-select-option value="02">采购</a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="行备注" name="freeTxt">
        <a-textarea style="width: 100%" v-model:value="form.freeTxt" placeholder="行备注" />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue';
import _ from 'lodash';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { bipordbApi } from '/@/api/business/erp/bipord-api';
import { smartSentry } from '/@/lib/smart-sentry';
import { excute } from '/@/utils/numformat-util.js';

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  id: undefined, //电键
  lineNum: undefined, //行号
  baseId: undefined, //基础电键
  baseLineNum: undefined, //基础行号
  lineStatus: undefined, //行状态
  freeTxt: undefined, //行备注
  itemId: undefined, //物料编码
  itemName: undefined, //物料名称
  specs: undefined, //规格
  unit: undefined, //单位
  onOrderQty: 0, //在途数量
  invQty: 0, //库存数量
  reqDate: null, //需求日期
  addQty: 0,
  reqQty: 0,
  source: undefined,
};

let form = reactive({ ...formDefault });

const rules = {};

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    if (form.id) {
      await bipordbApi.update(form);
    } else {
      await bipordbApi.add(form);
    }
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

function addQtyChange(item) {
  form.qty = form.reqQty + form.addQty - form.invQty;
}

defineExpose({
  show,
});
</script>
